<uieip type="page" value="base"></uieip>
<uieip type="reg" value="console,login,session,cache,http"></uieip>
<!doctype html>
<html>

<head>
    <uieip type="file" value="inc" src="/jsvm/inc/head.html"></uieip>
    <title>登录</title>
</head>

<body>
    <uieip type="file" value="exec" src="/jsvm/inc/title_exec.html"></uieip>
    <div class="container">
        <div class="row" style="margin-top:100px">
            <div class="col-md-offset-4 col-md-4 col-md-offset-3 col-md-5" style="padding: 30px;">
                <div class="panel panel-default">
                    <div class="panel-heading">用户登录</div>
                    <div class="panel-body">
                        <uieip>login_info</uieip>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <uieip type="file" value="inc" src="/jsvm/inc/foot.html"></uieip>
    <script runat="server">
        var backurl_link = "";
        var login_info = "";
        var app_id = "6f443b3811a52d3cc857e34765c71cd5";
        var app_corpsecret = "yq-5053cbe869be75ec54b10047431a5a8e";
        init();

        function init() {
            var backurl = request.param("backurl");
            if (login.state()) {
                if (backurl === null) {
                    response.redirect(request.baseurl());
                } else {
                    response.redirect(backurl);
                }
                return;
            }

            if (!(backurl === null)) {
                session.set("backurl", backurl);
            } else {
                backurl = session.getString("backurl");
                if (backurl === undefined) {
                    backurl = request.clienturl();
                }
            }

            backurl_link = backurl;

            var ticket = request.param("ticket");
            if (ticket === null) {
                response.redirect(ticket_get());
                return;
            }

            var res = get_userinfo(ticket);
            if (res.error == 0) {
                if (res.data.errcode == 0) {
                    login_check(res.data);
                } else {
                    login_info = "<p>" + res.data.errmsg + "[" + res.data.errcode + "]</p><a href=\"javascript:clear_get();\">重新登录</a>";
                }
            } else {
                login_info = "<p>" + res.info + "[" + res.error + "]</p><a href=\"javascript:clear_get();\">重新登录</a>";
            }
        }

        function get_userinfo(ticket) {
            var url = request.clienturl();
            var res = http.get("http://sso.uieip.com/connect/cas/servicevalidate?service=" + encodeURIComponent(url) + "&ticket=" + ticket + "&format=json", {
                "Accept": "application/json"
            });

            return res;
        }

        function login_check(userdata) {
            var ck = login.check(userdata.user);
            if (ck.error == 0) {
                backurl = session.getString("backurl");
                if (backurl === undefined) {
                    response.redirect(backurl_link);
                } else {
                    response.redirect(backurl);
                    session.delete("backurl");
                }
            } else if (ck.error == 2) {  //用户在本地不存在
                var url = request.clienturl();
                var res = http.post("http://sso.uieip.com/connect/cas/getuserdetail?service=" + encodeURIComponent(url), {
                    "userid": userdata.user
                }, {
                    "Accept": "application/json"
                });

                if (!(res.error == 0 && res.data.errcode == 0)) {
                    login_info = "<p>" + ck.info + "[" + ck.error + "]</p><a href=\"javascript:clear_get();\">重新登录</a>";
                }

                var data = res.data;
                /*
                    var row = {
                        user_type: 3,
                        user_id: data.attributes.userid,
                        name: data.attributes.name
                    };
                    if (!(data.attributes.phone === undefined || data.attributes.phone === null)) {
                        row.mobile = data.phone;
                    }
                    if (!(data.attributes.email === undefined || data.attributes.email === null)) {
                        row.email = data.email;
                    }

                    var res = login.addUser(row);
                    if (res.error == 0) {
                        login_check(data.userId);
                    } else {
                        login_info = "<p>" + ck.info + "[" + ck.error + "]</p><a href=\"javascript:clear_get();\">重新登录</a>";
                    }
                */
                login_info = "<p>" + ck.info + "[" + ck.error + "]</p><a href=\"javascript:clear_get();\">登录用户：" + data.login_name + " 不存在，重新登录</a>";
            } else {
                login_info = "<p>" + ck.info + "[" + ck.error + "]</p><a href=\"javascript:clear_get();\">重新登录</a>";
            }
        }

        function ticket_get() {
            var url = request.clienturl();
            return "http://sso.uieip.com/connect/cas/login?service=" + encodeURIComponent(url);
        }
    </script>
    <script>
        var backurl = "<uieip>backurl_link</uieip>";

        function clear_get() {
            location = "http://sso.uieip.com/login/logout.html?backurl=" + encodeURIComponent(backurl_get());
        }

        function backurl_get() {
            const url = new URL(location.href.replace(/#$/, ''));
            const _backurl = url.searchParams.get("backurl");
            return _backurl || backurl;
        }
    </script>
</body>

</html>